package com.xdkj.web.servlet;

import com.xdkj.commons.util.BaseResponse;
import com.xdkj.commons.util.ResponseEnum;
import com.xdkj.model.dao.TPositionDao;
import com.xdkj.model.dao.impl.TPositionDaoImpl;
import com.xdkj.model.entity.TPosition;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "PositionServlet", urlPatterns = "/position")
public class PositionServlet extends BaseServlet {
    private TPositionDao positionDao = new TPositionDaoImpl();

    /**
     * 查询所有职位
     *
     * @param request  请求
     * @param response 响应
     * @return {@link BaseResponse}
     * @throws ServletException servlet异常
     * @throws IOException      ioexception
     */
    public BaseResponse queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        BaseResponse baseResponse = null;
        List<TPosition> list = positionDao.queryAll();
        TPosition position = positionDao.queryByName(name);
        if (list.size() > 0) {
            Map<String, Object> map = new HashMap<>();
            map.put("list", list);
            map.put("position", position);
            baseResponse = BaseResponse.ok(ResponseEnum.QUERYSUCCESS.getCode(), ResponseEnum.QUERYSUCCESS.getMessage(), map);
        } else {
            baseResponse = BaseResponse.error(ResponseEnum.QUERYFAILURE.getCode(), ResponseEnum.QUERYFAILURE.getMessage());
        }
        return baseResponse;
    }
}